#include "main.h"
#include "Hardware_DWT.h" 

#define CPU_FREQ_MHZ 168

void DWT_Init(void)
{
	/* 使能DWT外设 */
	CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;

	/* DWT CYCCNT寄存器计数清0 */
	DWT->CYCCNT = 0u;
	/* 使能Cortex-M DWT CYCCNT寄存器 */
	DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
}

uint32_t DWT_GetNowTime_us(void)
{
	return (DWT->CYCCNT / CPU_FREQ_MHZ);
}

/**
  * @简  述  软件微秒延时
  * @参  数  us：延时长度，单位us
  * @返回值  无
  */
void DWT_Delayus(uint16_t _delay)
{	
	uint32_t tickstart = DWT->CYCCNT;
    uint32_t wait = _delay;
    while ((DWT->CYCCNT - tickstart) < wait * CPU_FREQ_MHZ);
}
